﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPPEI.Coolzon.UI.XProperty.Converters
{
    /// <summary>
    /// 画刷转换器
    /// 将 <ImageSource Url="..\\mn.png"/> 这种配置转换成画刷
    /// </summary>
    public class XImageSourceFileConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            string path = valueNode.Attributes["Url"].Replace("/", "\\").Replace(".\\", context.SolutionDirectory);
            BitmapImage bitmapImage = UI.SolutionResourceManager.GetImage(path);

            return new ImageSourceFile(bitmapImage, path);
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            Metadata.ValueNode vNode = new Metadata.ValueNode("ImageSourceFile");
            vNode.Attributes = new Dictionary<string, string>();
            ImageSourceFile imageSourceFile = (ImageSourceFile)value;
            string sourceString = imageSourceFile.Path.Replace("/", "\\").Replace(context.SolutionDirectory, ".\\");
            vNode.Attributes.Add("Url", sourceString);
            return vNode;
        }
    }
}
